package cn.iocoder.yudao.module.station.service.fillrecord;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.station.controller.admin.fillrecord.vo.*;
import cn.iocoder.yudao.module.station.dal.dataobject.fillrecord.FillRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.station.convert.fillrecord.FillRecordConvert;
import cn.iocoder.yudao.module.station.dal.mysql.fillrecord.FillRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.station.enums.ErrorCodeConstants.*;

/**
 * 充装记录 Service 实现类
 *
 * @author 林子
 */
@Service
@Validated
public class FillRecordServiceImpl implements FillRecordService {

    @Resource
    private FillRecordMapper fillRecordMapper;

    @Override
    public Long createFillRecord(FillRecordCreateReqVO createReqVO) {
        // 插入
        FillRecordDO fillRecord = FillRecordConvert.INSTANCE.convert(createReqVO);
        fillRecordMapper.insert(fillRecord);
        // 返回
        return fillRecord.getId();
    }

    @Override
    public void updateFillRecord(FillRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateFillRecordExists(updateReqVO.getId());
        // 更新
        FillRecordDO updateObj = FillRecordConvert.INSTANCE.convert(updateReqVO);
        fillRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteFillRecord(Long id) {
        // 校验存在
        validateFillRecordExists(id);
        // 删除
        fillRecordMapper.deleteById(id);
    }

    private void validateFillRecordExists(Long id) {
        if (fillRecordMapper.selectById(id) == null) {
            throw exception(FILL_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public FillRecordDO getFillRecord(Long id) {
        return fillRecordMapper.selectById(id);
    }

    @Override
    public List<FillRecordDO> getFillRecordList(Collection<Long> ids) {
        return fillRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<FillRecordDO> getFillRecordPage(FillRecordPageReqVO pageReqVO) {
        return fillRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<FillRecordDO> getFillRecordList(FillRecordExportReqVO exportReqVO) {
        return fillRecordMapper.selectList(exportReqVO);
    }

}
